﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using Niknak.Services.TaskScheduler.Interfaces.DataModel;

namespace Sandbox.Data.Mongo
{
    public class ScheduledTask : IScheduledTask
    {
        private static string COLLECTIONNAME = "ScheduledTask";

        [BsonId]
        public Guid ScheduledTaskId { get; set; }
        public Niknak.Services.TaskScheduler.Enums.TaskType TaskType { get; set; }
        public string Name { get; set; }
        public string DisplayName { get; set; }
        public string StartDate { get; set; }
        public string EndDate { get; set; }
        public string StartTime { get; set; }
        public string EndTime { get; set; }
        public bool TerminateAtEndTime { get; set; }
        public int IntervalType { get; set; }
        public long CustomIntervalSeconds { get; set; }
        public int RunOnDays { get; set; }
        public string CLRType { get; set; }
        public string StartArguments { get; set; }
        public bool IsActive { get; set; }
        public int AllowableDrift { get; set; }
        public bool EnableCompletedEvent { get; set; }
        public bool EnableLogging { get; set; }

        [BsonIgnore]
        public string EditPageUrl
        {
            get { return "TaskScheduler.aspx"; }
        }

        public bool Save()
        {
            Helper.GetCollection<ScheduledTask>(COLLECTIONNAME).Save(this);
            return true;
        }

        public static void RemoveAll()
        {
            Helper.GetCollection<ScheduledTask>(COLLECTIONNAME).RemoveAll();
        }

        public bool RemoveByTaskId(Guid taskId)
        {
            Helper.GetCollection<ScheduledTask>(COLLECTIONNAME).Remove(
                Query.EQ("_id", taskId)
            );
            new AuditLog().RemoveByTaskId(taskId);
            return true;
        }

        public static void InsertBatch(List<ScheduledTask> tasks)
        {
            Helper.GetCollection<ScheduledTask>(COLLECTIONNAME).InsertBatch(tasks);
        }

        public static void UpsertBatch(List<ScheduledTask> tasks)
        {
            foreach (ScheduledTask task in tasks)
                Helper.GetCollection<ScheduledTask>(COLLECTIONNAME).Save(task);
        }

        public IScheduledTask GetByTaskId(Guid taskId)
        {
            return Helper.GetCollection<ScheduledTask>(COLLECTIONNAME).FindOne(
                Query.EQ("_id", taskId)
            );
        }

        public IEnumerable<IScheduledTask> GetAllScheduledTasks()
        {
            return Helper.GetCollection<IScheduledTask>(COLLECTIONNAME)
                .FindAll()
                .Select(st => (IScheduledTask)st);
        }

        public static IEnumerable<ScheduledTask> GetScheduledTasks()
        {
            return Helper.GetCollection<ScheduledTask>(COLLECTIONNAME).FindAll();
        }
    }
}
